﻿using System;
using Besture.Bll;
using Besture.Common;
using Besture.Model;
using Besture.Utils;

namespace Besture.UI.Admin.Help
{
    public partial class HelpSet : BasePage
    {
        protected string type = Fun.Query("type");
        /// <summary>
        /// 
        /// </summary>
        public string _txtNContentValue = string.Empty;

        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            //页面加载
            InitPage();
            //编辑编号
            InitData(id);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {
            if (!string.IsNullOrEmpty(type))
            {
                trClassId.Attributes.Add("style","");
                ControlUtil.BindDropDownListClass(ddlClass, new HelpClassDac().GetDataTable(Common.Definition.Constants.HELP_CLASS_NOTICE), "HelpClassName", "ID");
            }
            else
            {
                trClassId.Attributes.Add("style", "display:none");
            }
           
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            if (id <= 0)
            {
                return;
            }
            HelpDac dac = new HelpDac();
            HelpModel model = dac.GetModel(id);
            HelpName.Text = model.HelpName;
            ddlClass.Text = model.HelpClassID.ToString();
            _txtNContentValue = model.HelpContext;
        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            HelpDac dac = new HelpDac();
            HelpModel model = null;
            //添加
            if (!EditPage)
            {
                model = new HelpModel();
                model.HelpName = HelpName.Text.Trim();
                model.HelpContext = Request.Params["editorcontent"];
                if (!string.IsNullOrEmpty(type))
                {
                    model.HelpClassID = Convert.ToInt32(ddlClass.Text);
                }
                else
                {
                    model.HelpClassID = Common.Definition.Constants.HELP_CLASS_NOTICE;
                }
                model.IsDeleted = 0;
                model.CreateTime = DateTime.Now;
                model.CreateBy = AdminUserService.AdminLoginUser.ID;
                model.UpdateBy = AdminUserService.AdminLoginUser.ID;
                model.UpdateTime = DateTime.Now;
                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(id);
                model.HelpName = HelpName.Text.Trim();
                model.HelpContext = Request.Params["editorcontent"];
                if (!string.IsNullOrEmpty(type))
                {
                    model.HelpClassID = Convert.ToInt32(ddlClass.Text);
                }
                model.IsDeleted = 0;
                model.UpdateBy = AdminUserService.AdminLoginUser.ID;
                model.UpdateTime = DateTime.Now;
                dac.Update(model);
            }
            if (!string.IsNullOrEmpty(type))
            {
                Fun.Alert("保存成功", "HelpList.aspx?type=0");
            }
            else
            {
                Fun.Alert("保存成功", "HelpList.aspx");
            }
            
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
            RedirectUrl("HelpList.aspx");
        }

        #endregion
    }
}